package ldk.util.slipview

import android.app.Activity
import android.os.Bundle
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

/**
 * Created by ldkxingzhe@163.com on 2017/6/27.
 */
@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class)
class ApplicationSingleTaskTest{

    lateinit var applicationSingleTask: ApplicationSingleTask

    @Before
    fun setUp(){
        applicationSingleTask = ApplicationSingleTask()
    }

    @Test
    fun test_currentActivity_null(){
        val currentActivity = applicationSingleTask.currentActivity()
        assertNull(currentActivity)
    }

    @Test
    fun test_currentActivity_nonNull(){
        val activity = Activity()
        applicationSingleTask.onActivityCreated(activity, null)
        applicationSingleTask.onActivityResumed(activity)
        assertEquals(activity, applicationSingleTask.currentActivity())
    }

    @Test
    fun test_previousActivity_null(){
        assertNull(applicationSingleTask.previousActivity())
        val activity = Activity()
        applicationSingleTask.onActivityCreated(activity, null)
        applicationSingleTask.onActivityResumed(activity)
        assertNull(applicationSingleTask.previousActivity())
    }

    @Test
    fun test_previousActivity_nonNull(){
        val activity1 = Activity()
        val activity2 = Activity()
        applicationSingleTask.onActivityCreated(activity1, null)
        applicationSingleTask.onActivityResumed(activity1)
        applicationSingleTask.onActivityPaused(activity1)
        applicationSingleTask.onActivityCreated(activity2, null)
        applicationSingleTask.onActivityResumed(activity2)
        applicationSingleTask.onActivityStopped(activity1)

        assertEquals(activity1, applicationSingleTask.previousActivity())
    }

    @Test
    fun test_currentActivity_MiddleDestroyed(){
        val activity1 = Activity()
        val activity2 = Activity()
        applicationSingleTask.onActivityCreated(activity1, null)
        applicationSingleTask.onActivityResumed(activity1)
        applicationSingleTask.onActivityPaused(activity1)
        applicationSingleTask.onActivityCreated(activity2, null)
        applicationSingleTask.onActivityResumed(activity2)
        applicationSingleTask.onActivityStopped(activity1)
        applicationSingleTask.onActivityDestroyed(activity1)
        assertEquals(activity2, applicationSingleTask.currentActivity())
        assertNull(applicationSingleTask.previousActivity())
    }

    @Test
    fun test_previousActivity_MiddleDestroyed(){
        val activity1 = Activity()
        val activity2 = Activity()
        val activity3 = Activity()
        applicationSingleTask.onActivityCreated(activity1, null)
        applicationSingleTask.onActivityResumed(activity1)
        applicationSingleTask.onActivityPaused(activity1)
        applicationSingleTask.onActivityCreated(activity2, null)
        applicationSingleTask.onActivityResumed(activity2)
        applicationSingleTask.onActivityStopped(activity1)
        applicationSingleTask.onActivityPaused(activity2)
        applicationSingleTask.onActivityCreated(activity3, null)
        applicationSingleTask.onActivityResumed(activity3)
        applicationSingleTask.onActivityStopped(activity2)
        applicationSingleTask.onActivityDestroyed(activity2)
        assertEquals(activity3, applicationSingleTask.currentActivity())
        assertEquals(activity1, applicationSingleTask.previousActivity())
    }

    @Test
    fun test_lastDestroyed(){
        val activity1 = Activity()
        val activity2 = Activity()
        val activity3 = Activity()
        applicationSingleTask.onActivityCreated(activity1, null)
        applicationSingleTask.onActivityResumed(activity1)
        applicationSingleTask.onActivityPaused(activity1)
        applicationSingleTask.onActivityCreated(activity2, null)
        applicationSingleTask.onActivityResumed(activity2)
        applicationSingleTask.onActivityStopped(activity1)
        applicationSingleTask.onActivityPaused(activity2)
        applicationSingleTask.onActivityCreated(activity3, null)
        applicationSingleTask.onActivityResumed(activity3)
        applicationSingleTask.onActivityStopped(activity2)
        applicationSingleTask.onActivityPaused(activity3)
        applicationSingleTask.onActivityStopped(activity3)
        applicationSingleTask.onActivityDestroyed(activity3)
        assertEquals(activity2, applicationSingleTask.currentActivity())
        assertEquals(activity1, applicationSingleTask.previousActivity())
    }

    @Test
    fun test_middleDestroyedBySystem(){
        val activity1 = Activity()
        val activity2 = Activity()
        val activity3 = Activity()
        val outBundle = Bundle()
        applicationSingleTask.onActivityCreated(activity1, null)
        applicationSingleTask.onActivityResumed(activity1)
        applicationSingleTask.onActivityPaused(activity1)
        applicationSingleTask.onActivityCreated(activity2, null)
        applicationSingleTask.onActivityResumed(activity2)
        applicationSingleTask.onActivityStopped(activity1)
        applicationSingleTask.onActivityPaused(activity2)
        applicationSingleTask.onActivityCreated(activity3, null)
        applicationSingleTask.onActivityResumed(activity3)
        applicationSingleTask.onActivitySaveInstanceState(activity2, outBundle)
        applicationSingleTask.onActivityStopped(activity2)
        applicationSingleTask.onActivityDestroyed(activity2)
        try {
            applicationSingleTask.previousActivity()
        }catch (e: IllegalStateException){
            // ignore
        }
        applicationSingleTask.onActivityCreated(activity2, outBundle)
        assertEquals(activity2, applicationSingleTask.previousActivity())
    }

    @Test
    fun test_middleDestroyedByYourSelf(){
        val activity1 = Activity()
        val activity2 = Activity()
        val activity3 = Activity()
        val activity4 = Activity()

        applicationSingleTask.onActivityCreated(activity1, null)
        applicationSingleTask.onActivityResumed(activity1)
        applicationSingleTask.onActivityPaused(activity1)
        applicationSingleTask.onActivityCreated(activity2, null)
        applicationSingleTask.onActivityResumed(activity2)
        applicationSingleTask.onActivityStopped(activity1)
        applicationSingleTask.onActivityPaused(activity2)
        applicationSingleTask.onActivityCreated(activity3, null)
        applicationSingleTask.onActivityResumed(activity3)
        applicationSingleTask.onActivityStopped(activity2)
        applicationSingleTask.onActivityPaused(activity3)
        applicationSingleTask.onActivityCreated(activity4, null)
        applicationSingleTask.onActivityResumed(activity4)
        applicationSingleTask.onActivityStopped(activity3)
        applicationSingleTask.onActivityDestroyed(activity3)
        applicationSingleTask.onActivityPaused(activity4)
        applicationSingleTask.onActivityResumed(activity2)
        applicationSingleTask.onActivityStopped(activity4)
        applicationSingleTask.onActivityDestroyed(activity4)
        assertEquals(activity1, applicationSingleTask.previousActivity())
    }

    @Test
    fun test_middleSaveInstanceState(){
        val activity1 = Activity()
        val activity2 = Activity()
        val activity3 = Activity()
        val activity4 = Activity()
        val outBundle = Bundle()

        applicationSingleTask.onActivityCreated(activity1, null)
        applicationSingleTask.onActivityResumed(activity1)
        applicationSingleTask.onActivityPaused(activity1)
        applicationSingleTask.onActivityCreated(activity2, null)
        applicationSingleTask.onActivityResumed(activity2)
        applicationSingleTask.onActivityStopped(activity1)
        applicationSingleTask.onActivityDestroyed(activity1)
        applicationSingleTask.onActivityPaused(activity2)
        applicationSingleTask.onActivityCreated(activity3, null)
        applicationSingleTask.onActivityResumed(activity3)
        applicationSingleTask.onActivitySaveInstanceState(activity2, outBundle)
        applicationSingleTask.onActivityStopped(activity2)
        applicationSingleTask.onActivityPaused(activity3)
        applicationSingleTask.onActivityStarted(activity2)
        applicationSingleTask.onActivityPaused(activity3)
        applicationSingleTask.onActivityCreated(activity4, null)
        applicationSingleTask.onActivityResumed(activity4)
        applicationSingleTask.onActivityStopped(activity3)
        applicationSingleTask.onActivityDestroyed(activity3)
        assertEquals(activity4, applicationSingleTask.currentActivity())
        assertEquals(activity2, applicationSingleTask.previousActivity())
    }

    @Test
    fun test_FirstDestroyed(){
        val activity1 = Activity();
        val activity2 = Activity();
        val activity3 = Activity();
        applicationSingleTask.onActivityCreated(activity1, null)
        applicationSingleTask.onActivityResumed(activity1)
        applicationSingleTask.onActivityPaused(activity1)
        applicationSingleTask.onActivityStopped(activity1)
        applicationSingleTask.onActivityDestroyed(activity1)
        applicationSingleTask.onActivityCreated(activity2, null)
        applicationSingleTask.onActivityResumed(activity2)
        applicationSingleTask.onActivityPaused(activity2)
        applicationSingleTask.onActivityCreated(activity3, null)
        applicationSingleTask.onActivityResumed(activity3)
        applicationSingleTask.onActivityPaused(activity3)
        applicationSingleTask.onActivityStopped(activity3)
        applicationSingleTask.onActivityDestroyed(activity3)
        applicationSingleTask.onActivityResumed(activity2)
        applicationSingleTask.onActivityPaused(activity2)
        applicationSingleTask.onActivitySaveInstanceState(activity2, Bundle())
        applicationSingleTask.onActivityStopped(activity2)
        applicationSingleTask.onActivityDestroyed(activity2)

    }

    @Test
    fun test_isFront(){
        assertFalse(applicationSingleTask.isInFront())
        val activity1 = Activity()
        val activity2 = Activity()
        applicationSingleTask.onActivityCreated(activity1, null)
        applicationSingleTask.onActivityResumed(activity1)
        assertTrue(applicationSingleTask.isInFront())
        applicationSingleTask.onActivityPaused(activity1)
        applicationSingleTask.onActivityStopped(activity1)
        applicationSingleTask.onActivityDestroyed(activity1)
        assertFalse(applicationSingleTask.isInFront())
        applicationSingleTask.onActivityResumed(activity1)
        assertTrue(applicationSingleTask.isInFront())
        applicationSingleTask.onActivityPaused(activity1)
        applicationSingleTask.onActivityCreated(activity2, null)
        applicationSingleTask.onActivityResumed(activity2)
        assertTrue(applicationSingleTask.isInFront())
        applicationSingleTask.onActivityStopped(activity2)
        assertFalse(applicationSingleTask.isInFront())
    }
}